package com.itheima.search.config;

import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author：shenkunlin
 * @date： 2021/9/2 16:02
 * @Description：
 ***/
@Component
public class SpringContext implements ApplicationContextAware {

    private static ApplicationContext act;

    /***
     * 将容器对象ApplicationContext传过来
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        act = applicationContext;
    }

    /***
     * 获取容器中指定类型的实例
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class clazz){
        return (T) act.getBean(clazz);
    }
}
